package com.example.cpl.gjc;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.VideoView;

public class MainActivity extends AppCompatActivity
{
    // Used to load the 'native-lib' library on application startup.
    static
    {
        System.loadLibrary("native-lib");
    }
    private VideoView mVideoView;
    private Button btn_search_grade,btn_clear_fault,btn_clear_grade,btn_test_manage,btn_device_manage,btn_user_manage,btn_format_test,btn_set_fault,button_main;      //声明控件
    private static final String TAG = "TAG";
    private Handler handler = new Handler();
    private TextView main_login_name,main_online_num;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

//        getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);      //    1
//        @SuppressLint("ResourceType")
//        Transition explode = TransitionInflater.from(MainActivity.this).inflateTransition(R.transition.explode);
//        getWindow().setExitTransition(explode);
//        getWindow().setEnterTransition(explode);
//        getWindow().setReenterTransition(explode);

        setContentView(R.layout.activity_main);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 

//        setTimerTask();         //初始化定时器

//        mVideoView= (VideoView) findViewById(R.id.videoView0);
//        Uri vUri = Uri.parse("android.resource://" + getPackageName() + "/" +R.raw.hn);
//        mVideoView.setVideoURI(vUri);
//        mVideoView.setMediaController(new MediaController(MainActivity.this));
//        mVideoView.start();
//
//        MediaController mc = new MediaController(this);     //隐藏videoview进度条或者控制部分
//        mc.setVisibility(View.INVISIBLE);
//        mVideoView.setMediaController(mc);
//        //监听视频播放完的代码
//        mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
//            @Override
//            public void onCompletion(MediaPlayer mPlayer) {
//                // TODO Auto-generated method stub
//                mPlayer.start();
//                mPlayer.setLooping(true);           //循环播放
//            }
//        });

        /*********     如果WiFi没有打开 就打开WiFi     ***********/
        WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();

        WifiAdmin wifiAdmin = new WifiAdmin(MainActivity.this);

        ConnectivityManager connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
        NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

        if((wifiInfo.getSSID().equals("<unknown ssid>"))||(!mWifi.isConnected()))
        {
            wifiAdmin.openWifi();
        }

        /****************************************************/
        //button_main=(Button)findViewById(R.id.button_main);
        btn_search_grade=(Button)findViewById(R.id.search_grade);
        //btn_clear_fault=(Button)findViewById(R.id.clear_fault);
        //btn_clear_grade=(Button)findViewById(R.id.clear_grade);
        btn_test_manage=(Button)findViewById(R.id.test_manage);
        //btn_device_manage=(Button)findViewById(R.id.device_manage);
        //btn_user_manage=(Button)findViewById(R.id.user_manage);
        //btn_format_test=(Button)findViewById(R.id.format_test);
        btn_set_fault=(Button)findViewById(R.id.set_fault);

        main_login_name=(TextView)findViewById(R.id.main_login_name);
        main_online_num=(TextView)findViewById(R.id.main_online_num);
        main_login_name.setText(teach_center.who);
        main_online_num.setText(""+device_manage.dev_num+"");


        //  btn_test_manage.setBackground("./p_1");
        btn_test_manage.setOnClickListener(new View.OnClickListener() {    //设置按钮监听
                                               @SuppressLint("PrivateResource")
                                               @Override
                                               public void onClick(View view) {
                                                   Intent intent = new Intent(MainActivity.this, button_2.class);
                                                   startActivity(intent);
                                                   overridePendingTransition(R.anim.abc_slide_in_bottom, R.anim.abc_slide_out_bottom);
                                               }
                                           }
        );
//        btn_device_manage.setOnClickListener(new View.OnClickListener() {    //设置按钮监听
//                 @SuppressLint("PrivateResource")
//                 @Override
//                 public void onClick(View view) {
//                     Intent intent = new Intent(MainActivity.this, device_manage.class);
//                     startActivity(intent);
//                     overridePendingTransition(R.anim.abc_slide_in_bottom, R.anim.abc_slide_out_bottom);
//                 }
//             }
//        );


        btn_set_fault.setOnClickListener(new View.OnClickListener() {    //设置按钮监听
               @SuppressLint("PrivateResource")
               @Override
               public void onClick(View view) {
    //                        mVideoView.pause();
                   Intent intent = new Intent(MainActivity.this, button_1.class);
                   startActivity(intent);
                   overridePendingTransition(R.anim.abc_slide_in_bottom, R.anim.abc_slide_out_bottom);
               }
           }
        );
//        btn_format_test.setOnClickListener(new View.OnClickListener() {
//            @SuppressLint("PrivateResource")
//            @Override
//            public void onClick(View view) {
////                mVideoView.pause();
//                Intent intent = new Intent(MainActivity.this, format_test.class);
//                startActivity(intent);
//                overridePendingTransition(R.anim.abc_slide_in_bottom, R.anim.abc_slide_out_bottom);
//            }
//        });
        btn_search_grade.setOnClickListener( new View.OnClickListener() {

            @SuppressLint("PrivateResource")
            @Override
            public void onClick(View view) {
//                mVideoView.pause();
                Intent intent = new Intent(MainActivity.this, button_3.class);
                startActivity(intent);
                overridePendingTransition(R.anim.abc_slide_in_bottom, R.anim.abc_slide_out_bottom);
            }
        });
//        btn_clear_grade.setOnClickListener(new View.OnClickListener() {
//            @SuppressLint("PrivateResource")
//            @Override
//            public void onClick(View view) {
////                mVideoView.pause();
//                Intent intent = new Intent(MainActivity.this, clear_grade.class);
//                startActivity(intent);
//                overridePendingTransition(R.anim.abc_slide_in_bottom, R.anim.abc_slide_out_bottom);
//            }
//        });
//        btn_clear_fault.setOnClickListener(new View.OnClickListener() {
//            @SuppressLint("PrivateResource")
//            @Override
//            public void onClick(View view) {
////                mVideoView.pause();
//                //Intent intent = new Intent(MainActivity.this, clear_fault.class);
//                Intent intent = new Intent(MainActivity.this, button_2.class);
//                startActivity(intent);
//                overridePendingTransition(R.anim.abc_slide_in_bottom, R.anim.abc_slide_out_bottom);
//            }
//        });

//        btn_user_manage.setOnClickListener(new View.OnClickListener() {    //设置按钮监听
//                                               @SuppressLint("PrivateResource")
//                                               @Override
//                                               public void onClick(View view) {
//                                                   Intent intent = new Intent(MainActivity.this, user_manage.class);
//                                                   startActivity(intent);
//                                                   overridePendingTransition(R.anim.abc_slide_in_bottom, R.anim.abc_slide_out_bottom);
//                                               }
//                                           }
//        );


//        button_main.setOnClickListener(new View.OnClickListener() {
//            @SuppressLint("PrivateResource")
//            @Override
//            public void onClick(View view) {
//
////                Intent intent = new Intent(MainActivity.this, Main3Activity.class);
////                startActivity(intent);
////                overridePendingTransition(R.anim.abc_slide_in_bottom, R.anim.abc_slide_out_bottom);
//
//                startActivity(new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS));  //跳转到系统WiFi界面
//                overridePendingTransition(R.anim.abc_slide_in_bottom, R.anim.abc_slide_out_bottom);
//            }
//        });



        // Example of a call to a native method

    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }




    //    @Override
//    protected void onResume() {
//        super.onResume();
// try {
//    socket.close();             //断开TCP
//} catch (IOException e) {
//    e.printStackTrace();
//}
//
//    }
//    java.util.Timer timer = new java.util.Timer(true);
//    private void setTimerTask() {
//        timer.schedule(new TimerTask() {
//                           @Override
//                           public void run() {
//                               WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
//                               final WifiInfo wifiInfo = wifiManager.getConnectionInfo();
//                               if((wifiInfo.getSSID()).equals("\"gjc1\""))
//                               {
//                                   ConnectivityManager connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
//                                   NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
//                                   if (mWifi.isConnected()) {
//                                       handler.post(new Runnable() {//在子线程中调用Handler对象更新
//                                           @Override
//                                           public void run() {
//
//                                               button1.setText("go to");   //在此处直接更新UI，例如mText.setText("更新")；
//                                           }
//                                       });
//                                   }
//                                   else {
//                                       handler.post(new Runnable() {//在子线程中调用Handler对象更新
//                                           @Override
//                                           public void run() {
//                                               button1.setText("连接中..");
//                                           }
//                                       });
//                                   }
//                               }
//                               else
//                               {
//                                   handler.post(new Runnable() {//在子线程中调用Handler对象更新
//                                       @Override
//                                       public void run() {
//                                           button1.setText("一号车");
//                                       }
//
//                                   });
//                               }
//
//
//                               if((wifiInfo.getSSID()).equals("\"gjc2\""))
//                               {ConnectivityManager connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
//                                   NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
//                                   if (mWifi.isConnected()) {
//                                   handler.post(new Runnable() {//在子线程中调用Handler对象更新
//                                       @Override
//                                       public void run() {
//                                           button2.setText("go to");   //在此处直接更新UI，例如mText.setText("更新")；
//                                       }
//                                   });}else {
//                                       handler.post(new Runnable() {//在子线程中调用Handler对象更新
//                                           @Override
//                                           public void run() {
//                                               button2.setText("连接中..");
//
//                                           }
//                                       });
//
//                                   }
//                               }
//                               else
//                               {
//                                   handler.post(new Runnable() {//在子线程中调用Handler对象更新
//                                       @Override
//                                       public void run() {
//                                           button2.setText("二号车");
//                                       }
//                                   });
//                               }
//                               if((wifiInfo.getSSID()).equals("\"gjc3\""))
//                               {ConnectivityManager connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
//                                   NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
//                                   if (mWifi.isConnected()) {
//                                   handler.post(new Runnable() {//在子线程中调用Handler对象更新
//                                       @Override
//                                       public void run() {
//                                           button3.setText("go to");   //在此处直接更新UI，例如mText.setText("更新")；
//                                       }
//                                   });}else {
//                                       handler.post(new Runnable() {//在子线程中调用Handler对象更新
//                                           @Override
//                                           public void run() {
//                                               button3.setText("连接中..");
//                                           }
//                                       });
//                                   }
//                               }
//                               else
//                               {
//                                   handler.post(new Runnable() {//在子线程中调用Handler对象更新
//                                       @Override
//                                       public void run() {
//
//                                           button3.setText("三号车");
//                                       }
//                                   });
//                               }
//
//                           }
//                       }, 1500, 1500/* 表示1000毫秒之後，每隔1000毫秒執行一次 */
//        );
//    }
    @Override
    protected void onRestart() {
        super.onRestart();

        setContentView(R.layout.activity_main);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

//        mVideoView= (VideoView) findViewById(R.id.videoView0);
//        Uri vUri = Uri.parse("android.resource://" + getPackageName() + "/" +R.raw.hn);
//        mVideoView.setVideoURI(vUri);
//        mVideoView.setMediaController(new MediaController(MainActivity.this));
//
//        mVideoView.start();
//        MediaController mc = new MediaController(this);     //隐藏videoview进度条或者控制部分
//        mc.setVisibility(View.INVISIBLE);
//        mVideoView.setMediaController(mc);
//
//        //监听视频播放完的代码
//        mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
//            @Override
//            public void onCompletion(MediaPlayer mPlayer) {
//                // TODO Auto-generated method stub
//                mPlayer.start();
//                mPlayer.setLooping(true);           //循环播放
//            }
//        });

       // button_main=(Button)findViewById(R.id.button_main);
        btn_search_grade=(Button)findViewById(R.id.search_grade);
        //btn_clear_fault=(Button)findViewById(R.id.clear_fault);
        //btn_clear_grade=(Button)findViewById(R.id.clear_grade);
        btn_test_manage=(Button)findViewById(R.id.test_manage);
        //btn_device_manage=(Button)findViewById(R.id.device_manage);
       // btn_user_manage=(Button)findViewById(R.id.user_manage);
        //btn_format_test=(Button)findViewById(R.id.format_test);
        btn_set_fault=(Button)findViewById(R.id.set_fault);

        main_login_name=(TextView)findViewById(R.id.main_login_name);
        main_online_num=(TextView)findViewById(R.id.main_online_num);
        main_login_name.setText(teach_center.who);
        main_online_num.setText(""+device_manage.dev_num+"");

        btn_test_manage.setOnClickListener(new View.OnClickListener() {    //设置按钮监听
                                                 @SuppressLint("PrivateResource")
                                                 @Override
                                                 public void onClick(View view) {
                                                     Intent intent = new Intent(MainActivity.this, button_2.class);
                                                     startActivity(intent);
                                                     overridePendingTransition(R.anim.abc_slide_in_bottom, R.anim.abc_slide_out_bottom);
                                                 }
                                             }
        );
//        btn_user_manage.setOnClickListener(new View.OnClickListener() {    //设置按钮监听
//                                                 @SuppressLint("PrivateResource")
//                                                 @Override
//                                                 public void onClick(View view) {
//                                                     Intent intent = new Intent(MainActivity.this, user_manage.class);
//                                                     startActivity(intent);
//                                                     overridePendingTransition(R.anim.abc_slide_in_bottom, R.anim.abc_slide_out_bottom);
//                                                 }
//                                             }
//        );
//        btn_device_manage.setOnClickListener(new View.OnClickListener() {    //设置按钮监听
//                                                 @SuppressLint("PrivateResource")
//                                                 @Override
//                                                 public void onClick(View view) {
//                                                     Intent intent = new Intent(MainActivity.this, device_manage.class);
//                                                     startActivity(intent);
//                                                     overridePendingTransition(R.anim.abc_slide_in_bottom, R.anim.abc_slide_out_bottom);
//                                                 }
//                                             }
//        );
        btn_set_fault.setOnClickListener(new View.OnClickListener() {    //设置按钮监听
                                             @SuppressLint("PrivateResource")
                                             @Override
                                             public void onClick(View view) {
//                        mVideoView.pause();
                                                 Intent intent = new Intent(MainActivity.this, button_1.class);
                                                 startActivity(intent);
                                                 overridePendingTransition(R.anim.abc_slide_in_bottom, R.anim.abc_slide_out_bottom);
                                             }
                                         }
        );
//        btn_format_test.setOnClickListener(new View.OnClickListener() {
//            @SuppressLint("PrivateResource")
//            @Override
//            public void onClick(View view) {
////                mVideoView.pause();
//                Intent intent = new Intent(MainActivity.this, format_test.class);
//                startActivity(intent);
//                overridePendingTransition(R.anim.abc_slide_in_bottom, R.anim.abc_slide_out_bottom);
//            }
//        });
        btn_search_grade.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("PrivateResource")
            @Override
            public void onClick(View view) {
//                mVideoView.pause();
                Intent intent = new Intent(MainActivity.this, button_3.class);
                startActivity(intent);
                overridePendingTransition(R.anim.abc_slide_in_bottom, R.anim.abc_slide_out_bottom);
            }
        });
//        btn_clear_grade.setOnClickListener(new View.OnClickListener() {
//            @SuppressLint("PrivateResource")
//            @Override
//            public void onClick(View view) {
////                mVideoView.pause();
//                Intent intent = new Intent(MainActivity.this, clear_grade.class);
//                startActivity(intent);
//                overridePendingTransition(R.anim.abc_slide_in_bottom, R.anim.abc_slide_out_bottom);
//            }
//        });
//        btn_clear_fault.setOnClickListener(new View.OnClickListener() {
//            @SuppressLint("PrivateResource")
//            @Override
//            public void onClick(View view) {
////                mVideoView.pause();
//                Intent intent = new Intent(MainActivity.this, button_2.class);
//                startActivity(intent);
//                overridePendingTransition(R.anim.abc_slide_in_bottom, R.anim.abc_slide_out_bottom);
//            }
//        });


//        button_main.setOnClickListener(new View.OnClickListener() {
//            @SuppressLint("PrivateResource")
//            @Override
//            public void onClick(View view) {
//
////                Intent intent = new Intent(MainActivity.this, Main3Activity.class);
////                startActivity(intent);
////                overridePendingTransition(R.anim.abc_slide_in_bottom, R.anim.abc_slide_out_bottom);
//
//                startActivity(new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS));  //跳转到系统WiFi界面
//                overridePendingTransition(R.anim.abc_slide_in_bottom, R.anim.abc_slide_out_bottom);
//            }
//        });



    }
    //    @Override
//    protected void onResume() {
//        /**
//         * 设置为横屏
//         */
//        if(getRequestedOrientation()!= ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE){
//            setRequestedOrientation(ActivityInfo.SCREEN1++_ORIENTATION_LANDSCAPE);
//        }
//        super.onResume();
//    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    /**
     * A native method that is implemented by the 'native-lib' native library,
     * which is packaged with this application.
     */
    public native String stringFromJNI();
}
